﻿Imports System.ComponentModel
Imports System.Linq
Imports System.Windows.Forms

Public MustInherit Class ActionManagerBase

#Region "variables"
    Private m_Timer As Timer = Nothing

    Protected m_Index As Integer = 0
    Protected m_ActionDic As Dictionary(Of String, ActionBase) = Nothing
#End Region

#Region "properties"


#End Region

#Region "methods"

    '[private]------------------------------------------------------------
    Private Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
        Me.ChangeAction()
    End Sub

    Private Sub Play()
        Dim _Action As ActionBase = Me.Next()
        _Action.Play()
        Me.ChangeTimerInterval(_Action.Duration)
    End Sub

    '[protected]----------------------------------------------------------
    ''' <summary>
    ''' 改变时钟的间隔
    ''' </summary>
    ''' <param name="value">间隔值，单位：毫秒</param>
    Protected Sub ChangeTimerInterval(ByVal value As Integer)
        Me.m_Timer.Interval = value
    End Sub

    '[public]-------------------------------------------------------------
    Public Sub [Add](ByVal action As ActionBase)
        Me.m_ActionDic.Add(action.Name, action)
    End Sub

    Public MustOverride Function [Next]() As ActionBase

    Public Sub Init()
        For Each _Action As ActionBase In Me.m_ActionDic.Values
            _Action.Init()
        Next
    End Sub

    Public Sub Init(ByVal state As Object)
        For Each _Action As ActionBase In Me.m_ActionDic.Values
            _Action.Init(state)
            Threading.Thread.Sleep(500)
        Next
    End Sub

    ''' <summary>
    ''' 开始
    ''' </summary>
    Public Sub [Start]()
        Me.m_Timer = New Timer() With {.Enabled = True}
        AddHandler Me.m_Timer.Tick, AddressOf Me.Timer_Tick

        Me.Play()
    End Sub

    Public Sub [Stop]()
        Me.m_Timer.Enabled = False
    End Sub

    ''' <summary>
    ''' 退出
    ''' </summary>
    Public Overridable Sub [Exit]()
        Me.m_Timer.Dispose()
        Me.m_Timer = Nothing
    End Sub

    ''' <summary>
    ''' 改变动作
    ''' </summary>
    Public Sub ChangeAction()
        Dim _RunActions = From _RunAction In Me.m_ActionDic.Values _
                          Where _RunAction.IsBusy = True _
                          Select _RunAction
        For Each _Action As ActionBase In _RunActions
            _Action.Stop()
        Next

        Me.Play()
    End Sub


#End Region

    Public Sub New()
        Me.m_ActionDic = New Dictionary(Of String, ActionBase)()
    End Sub


End Class
